package com.wyw.hemerocallis.stream;

import com.alibaba.fastjson.JSON;
import com.wyw.hemerocallis.vo.WywMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 使用默认的通信信道实现消息的发送
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
@Slf4j
@EnableBinding(Source.class)  //指定一个或者多个的input或者output注解的接口
public class DefaultSendService {

    private final Source source;

    public DefaultSendService(Source source) {
        this.source = source;
    }

    public void sendMessage(WywMessage message) {
        String _message = JSON.toJSONString(message);
        log.info("in DefaultSendService send message: [{}]", _message);

        //Spring Messaging，统一消息的编程模型，是Stream组件的重要组成部分之一
        source.output().send(MessageBuilder.withPayload(_message).build());
    }

}
